""""
Case Type   : D库常用语法兼容-proc
Case Name   : D库支持创建存储过程。rename后执行正常，部分测试点合理报错
Create At   : 2025-11-19
Owner       : chen
Description :
    1.创建proc
    2.rename proc
    3.rename procedure
    4.alter rename 不带括号
    5.删除不存在的存储过程
    6.删除存储过程
Expect      :
    1.成功
    2.成功
    3.成功
    4.失败 
    5.失败
    6.成功 
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class DDL_Proc_Case0001(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.dbname_d = Node('PrimaryDbDmode').db_name
        self.proc_name = 'p_proc_case001'
        self.new_proc_name = 'p_new_proc_case001'

    def test_Proc(self):
        text = '-------------step1: 创建proc; expect: 成功-------------'
        log.info(text)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_proc = f"""create proc {self.proc_name}()
            is
            begin
            RAISE INFO 'call procedure: {self.proc_name}';
            end;
        """
        res = primary_sh.execut_db_sql(create_proc, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, res)
        detail_sql = f'\df {self.proc_name}();'
        res = primary_sh.execut_db_sql(detail_sql, dbname=self.dbname_d)
        log.info(res)
        detail_list = ['public', self.proc_name, 'void', 'normal', 'f', 'p']
        for detail in detail_list:
            self.assertIn(detail, res)
        find_sql = f"select provolatile from pg_proc where proname =" \
            f" '{self.proc_name}';"
        res = primary_sh.execut_db_sql(find_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('v', res)

        text = '-------------step2: rename proc; expect: 成功-------------'
        log.info(text)
        alter_sql = f'alter proc {self.proc_name}() rename to ' \
            f'{self.new_proc_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)
        detail_sql = f'\df {self.new_proc_name}();'
        res = primary_sh.execut_db_sql(detail_sql, dbname=self.dbname_d)
        log.info(res)
        detail_list = ['public', self.new_proc_name, 'void', 'normal', 'f', 'p']
        for detail in detail_list:
            self.assertIn(detail, res)
        find_sql = f"select provolatile from pg_proc where proname =" \
            f" '{self.new_proc_name}';"
        res = primary_sh.execut_db_sql(find_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('v', res)
        alter_sql = f'alter proc {self.new_proc_name}() rename to ' \
            f'{self.proc_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)

        text = '-------------step3: rename procedure; expect: 成功-------------'
        log.info(text)
        alter_sql = f'alter procedure {self.proc_name}() rename to ' \
            f'{self.new_proc_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)
        alter_sql = f'alter procedure {self.new_proc_name}() rename to ' \
            f'{self.proc_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)

        text = '-------------step4: alter rename 不带括号; expect: 合理报错-------------'
        log.info(text)
        alter_sql = f'alter proc {self.proc_name} rename to ' \
            f'{self.new_proc_name};'
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('ERROR:  syntax error', res)

        text = '-------------step5: 删除不存在的存储过程; expect: 合理报错-------------'
        log.info(text)
        drop_sql = f'drop proc {self.new_proc_name}();'
        res = primary_sh.execut_db_sql(drop_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('ERROR:', res)
        self.assertIn('does not exist', res)

    def tearDown(self):
        text = '-------------step6: 删除存储过程; expect: 成功-------------'
        log.info(text)
        drop_sql = f'drop proc if exists {self.proc_name}();' \
            f'drop proc if exists {self.new_proc_name}();'
        res = primary_sh.execut_db_sql(drop_sql, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.DROP_PROCEDURE_SUCCESS_MSG, res)
